from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Enum
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from database import Base
import enum


class LogStatus(str, enum.Enum):
    SUCCESS = "success"    # 发送成功
    FAILED = "failed"      # 发送失败
    PENDING = "pending"    # 待发送


class EmailLog(Base):
    __tablename__ = "email_logs"
    
    id = Column(Integer, primary_key=True, index=True)
    task_id = Column(Integer, ForeignKey("email_tasks.id"), nullable=False)
    recipient_email = Column(String(100), nullable=False, index=True)
    status = Column(Enum(LogStatus), nullable=False)
    error_message = Column(Text, nullable=True)
    sent_at = Column(DateTime(timezone=True), nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关系
    task = relationship("EmailTask", backref="email_logs")
    
    def __repr__(self):
        return f"<EmailLog(id={self.id}, recipient='{self.recipient_email}', status='{self.status}')>"
